import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';

class EditTextView extends StatefulWidget {
  /// 宽度
  final double widget;

  /// 高度
  final double height;

  /// 背景的圆角大小
  final double borderRadius;

  /// 背景颜色
  final Color backgroundColor;

  /// 输入框左侧图标  建议用Image
  final Widget? leftImage;

  /// 输入框左侧的图标
  final Icon? leftIcon;

  /// 输入框右侧图标
  final Widget? rightImage;

  /// 输入框右侧图标
  final Icon? rightIcon;

  /// 右侧按钮点击事件
  final Function? rightClick;

  /// 提示文字
  final String hintText;

  /// 提示文字样式
  final TextStyle? hintStyle;

  /// 内容文字样式
  final TextStyle? textStyle;

  /// 最大行数
  final int? maxLines;

  /// 是否是加密文字
  final bool obscureText;

  /// 软键盘类型
  final TextInputType? keyboardType;

  /// 输入框控制器
  final TextEditingController controller;

  ///
  final TextInputAction? textInputAction;
  final ValueChanged<String>? onSubmitted;
  final ValueChanged<String>? onChange;


  const EditTextView({
    Key? key,
    required this.controller,
    required this.widget,
    required this.height,
    this.borderRadius = 0,
    this.backgroundColor = Colors.grey,
    this.leftImage,
    this.leftIcon,
    this.rightImage,
    this.rightIcon,
    this.rightClick,
    this.hintText = '',
    this.hintStyle,
    this.textStyle,
    this.maxLines,
    this.obscureText = false,
    this.keyboardType, this.textInputAction, this.onSubmitted, this.onChange,
  }) : super(key: key);

  @override
  _EditTextViewState createState() => _EditTextViewState();
}

class _EditTextViewState extends State<EditTextView> {
  @override
  Widget build(BuildContext context) {
    bool hasLeftIcon = widget.leftImage != null || widget.leftIcon != null;
    bool hasRightIcon = widget.rightImage != null || widget.rightIcon != null;

    return Container(
      width: widget.widget,
      height: widget.height,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(widget.borderRadius),
        color: widget.backgroundColor,
      ),
      child: Row(
        children: [
          Get.widthBox(10.dp),

          /// 左侧图标
          widget.leftImage ?? widget.leftIcon ?? Get.widthBox(0.dp),
          hasLeftIcon ? Get.widthBox(10.dp) : Get.widthBox(0.dp),

          /// 输入框
          Expanded(
            child: TextField(
              controller: widget.controller,
              decoration: InputDecoration(
                border: OutlineInputBorder(borderSide: BorderSide.none),
                hintText: widget.hintText,
                hintMaxLines: 1,
                hintStyle: widget.hintStyle,
                contentPadding: EdgeInsets.zero,
              ),
              style: widget.textStyle,
              maxLines: widget.maxLines,
              obscureText: widget.obscureText,
              keyboardType: widget.keyboardType,
              onChanged: (v){
                setState(() {

                });
                if(widget.onChange != null) {
                  widget.onChange!(v);
                }
              },
              textInputAction: widget.textInputAction,
                onSubmitted: widget.onSubmitted,
            ),
          ),
          Get.widthBox(10.dp),

          /// 右侧清除图标
          hasRightIcon
              ? Visibility(
                  visible: widget.controller.text.length > 0,
                  child: InkWell(
                    onTap: (){
                      if(widget.rightClick != null) {
                        widget.rightClick!();
                        setState(() {

                        });
                      }
                    },
                    child: widget.rightImage ?? widget.rightIcon!,
                  ),
                )
              : Get.widthBox(0),
          Get.widthBox(hasRightIcon ? 10.dp : 0),
        ],
      ),
    );
  }
}
